Java 21 – What’s New in the Latest LTS?

An overview of the most important changes and features in Java 21 – including virtual threads, pattern matching, sealed classes, and much more.

While we often hear that Java 21 is simply "the latest LTS," the reality is a bit more complex. Java 21 is a set of specifications that describe the behavior of the language, virtual machine, libraries, etc. The reference implementation (OpenJDK) is developed in the OpenJDK repository, and after its release, the JDK21U branch is created to receive security updates and bug fixes.

However, this does not mean that JDK21U will be supported forever. This is where JDK distributors come into play, such as Eclipse Temurin, Oracle, Red Hat, Azul, or Amazon, who create their own JDK binaries and often offer commercial support. In practice, "Java 21 as an LTS" is more of a vendor declaration than part of the language specification.

In this article, I focus on migrating from Java 17 to Java 21 and selected features. If you're wondering whether it's worth switching to the new version or starting a new project with it – this post is for you!

Virtual Threads

Virtual threads are the biggest change in concurrency since the introduction of ForkJoinPool. In the traditional Java model, threads were mapped to operating system threads. This approach works, but it’s costly – OS threads consume a lot of memory and have limited capacity.

Each thread can be blocked for a long time, e.g., while waiting for an HTTP response, even though actual processing takes very little time. Until now, this was typically solved using asynchronous code, which is often harder to maintain.

Virtual threads offer a new approach – allowing you to write synchronous code with asynchronous-like behavior, without the overhead of OS threads.

Example result (system threads):


Number of threads = 1000, Time (ms) = 1100  
Number of threads = 10000, Time (ms) = 1600  
Number of threads = 100000, Time (ms) = 5300  
Critical error: stack memory limit exceeded
          

For a large number of threads, the program crashes or throws an OutOfMemoryError.

Version with virtual threads (VirtualThreads.java):


Number of threads = 1000, Time (ms) = 1020  
Number of threads = 10000, Time (ms) = 1056  
Number of threads = 100000, Time (ms) = 1106  
Number of threads = 1000000, Time (ms) = 1806  
Number of threads = 10000000, Time (ms) = 22010
          

Unlike the previous approach, the application doesn’t crash and can easily handle millions of threads simultaneously.

Sequenced Collections

Java has long offered various collection implementations – List, Set, Map – but not all guaranteed consistent element processing in a defined order.

Java 21 introduces a new interface: SequencedCollection, which standardizes the way collections with a logical element order are handled.

What is SequencedCollection?


public interface SequencedCollection<E> extends Collection<E> {
    E getFirst();
    E getLast();
    SequencedCollection<E> reversed();
}
          

There are also:

  • SequencedSet<E> – e.g., LinkedHashSet
  • SequencedMap<K,V> – e.g., LinkedHashMap

This allows you to iterate through maps in reverse order — without manually reversing entries. It’s an example of how Java’s API evolves – instead of changing existing classes, lightweight, semantic interfaces are added.

Pattern Matching for switch

For many years, Java’s switch was a simple flow control construct for primitive values (int, enum, String). In Java 21, it becomes even more powerful thanks to pattern matching.

What is a pattern in switch?

It allows matching an object’s type and unpacking it into a local variable, with optional guard clauses. No more instanceof checks and casting.

Patterns with guard clauses:

You can add conditions to individual cases:


case Point p when p.x() > 0 -> System.out.println("Point is to the right");
          

Pattern matching for switch enables:

  • less type casting,
  • more readable code,
  • full compatibility with sealed types and records,
  • completeness checks by the compiler.

Sealed Classes

Inheritance in Java has always been open. Java 17 introduced the sealed modifier, and in Java 21, it has been fully stabilized.

What are Sealed Classes?

They allow defining the exact list of classes that can extend a given base class.


public sealed class Shape permits Circle, Rectangle {}
public final class Circle extends Shape {}
public final class Rectangle extends Shape {}
          

Benefits include:

  • control over the type hierarchy,
  • the compiler knows all possible subclasses,
  • no need for default in switch,
  • great synergy with record and pattern matching.

Conclusion

Java 21 is more than just a bundle of "new features" – it’s a step toward a modern, concise programming style.

It simplifies code without losing clarity. If you're still using Java 11 or 17, upgrading to Java 21 brings real benefits — in both performance and quality.

It’s worth experimenting and bringing these features into production code.

Dane firmy

Codepred Spółka Cywilna
NIP: PL7812070299
REGON: 528624080

Obserwuj nas

© codepred.pl - All Rights Reserved